package com.roncoo.education.course.service.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.roncoo.education.course.common.bo.CourseListByTagAndCategoryBO;
import com.roncoo.education.course.service.dao.CourseAuditDao;
import com.roncoo.education.course.service.dao.impl.mapper.CourseAuditMapper;
import com.roncoo.education.course.service.dao.impl.mapper.CourseTabMapper;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseAudit;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseAuditExample;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseAuditExample.Criteria;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseTag;
import com.roncoo.education.util.tools.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class CourseAuditDaoImpl implements CourseAuditDao {
    @Autowired
    private CourseAuditMapper courseAuditMapper;
    @Autowired
    private CourseTabMapper courseTabMapper;

    public int save(CourseAudit record) {
        record.setId(IdWorker.getId());
        return this.courseAuditMapper.insert(record);
    }

    public int deleteById(Long id) {
        return this.courseAuditMapper.deleteById(id);
    }

    public int updateById(CourseAudit record) {
    	record.setGmtCreate(null);
		record.setGmtModified(null);
        return this.courseAuditMapper.updateById(record);
    }

    public CourseAudit getById(Long id) {
        return this.courseAuditMapper.selectById(id);
    }

//    public Page<CourseAudit> listForPage(int pageCurrent, int pageSize, CourseAuditExample example) {
//        int count = this.courseAuditMapper.countByExample(example);
//        pageSize = PageUtil.checkPageSize(pageSize);
//        pageCurrent = PageUtil.checkPageCurrent(count, pageSize, pageCurrent);
//        int totalPage = PageUtil.countTotalPage(count, pageSize);
//        example.setLimitStart(PageUtil.countOffset(pageCurrent, pageSize));
//        example.setPageSize(pageSize);
//        return new Page<CourseAudit>(count, totalPage, pageCurrent, pageSize, this.courseAuditMapper.selectByExample(example));
//    }

    public Page<CourseAudit> listForPage(int pageCurrent, int pageSize, QueryWrapper queryWrapper) {
        Page<CourseAudit> courseAuditPage = new Page<CourseAudit>(pageCurrent, pageSize);
        Page page = this.courseAuditMapper.selectPage(courseAuditPage, queryWrapper);
        return page;
    }

	@Override
	public int updateAuditStatusBycourseId(Integer auditStatus, Long courseId) {
//		CourseAuditExample example = new CourseAuditExample();
//		Criteria c = example.createCriteria();
//		c.andIdEqualTo(courseId);
        QueryWrapper<CourseAudit> id = new QueryWrapper<CourseAudit>().eq("id", courseId);
        CourseAudit record = new CourseAudit();
		record.setId(courseId);
		record.setAuditStatus(auditStatus);
		record.setGmtCreate(null);
		record.setGmtModified(null);
		return this.courseAuditMapper.update(record, id);
	}

    @Override
    public List<CourseAudit> listByTag(CourseListByTagAndCategoryBO courseBO) {
        List<Long> courseIds=null;
        if(courseBO.getTagId()!=null){
            QueryWrapper<CourseTag> tag_id = new QueryWrapper<CourseTag>().eq("tag_id", courseBO.getTagId());
            List<CourseTag> courseTags = courseTabMapper.selectList(tag_id);
            courseIds = courseTags.stream().map(CourseTag::getCourseId).collect(Collectors.toList());//去除tagid
        }
        QueryWrapper<CourseAudit> courseAuditQueryWrapper = new QueryWrapper<>();
//        if(!courseBO.isAllCateGory()){
//            //若不是查询全部分类
//            courseAuditQueryWrapper.eq(courseBO.getCategoryId()!=null, BOField.CATEGORY_ID1,courseBO.getCategoryId());
//        }
        courseAuditQueryWrapper.in(courseIds!=null,"id",courseIds);
        List<CourseAudit> courseAudits = this.courseAuditMapper.selectList(courseAuditQueryWrapper);
        return courseAudits;
    }

    @Override
    public List<CourseAudit> list(QueryWrapper<CourseAudit> wrapper) {
        return this.courseAuditMapper.selectList(wrapper);
    }

}